<?php
namespace Cust\Controller;
use Common\Controller\CommonController;

/**
 * 登录注册入口模块
 */
class InitController extends CommonController {
	
	protected function _initialize(){
		parent::_initialize();
		$this->_moduleid = 100;
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_cust = new \Common\Model\CustModel();
	}
	
	public function decodetokenAction(){
		$_token = I('request._token');
		$_token = str_ireplace(" " , "+" , $_token);
		$_token_json = \aes::decrypt($_token, C('TOKEN_SALT'));
		$_token_arr = json_decode($_token_json , true);
		print_r($_token_arr);
	}
	
    
	/**
	 * 登录
	 */
    public function loginAction(){
		$username = I('request.username');
		$password = I('request.password');
		$time = I('request.time');
		$sign = I('request.sign');
		$logintype = (int)I('request.logintype'); //1为app登录  2为web登录 933.内部服务调用
		$this->_login($username,$password,$time,$sign,$logintype);
    }
    
    private function _login($username,$password,$time,$sign,$logintype){
    	if(empty($logintype)) $logintype = 1;
    	if(empty($username) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}else{
    		$this->_checkSign($username,$time,$sign);
    		 
    		//判断是否被暂时禁止，防止暴力登录
    		$failureTimesKey = \cachekey::$key['BASE_KEY_CONF']['FAILURE_LOGIN_TIMES'].$username;
    		$failureTimes = (int)$this->_mycache->get($failureTimesKey);
    		if($failureTimes > (int)C('PER_DAY_MAX_ERROR_LOGIN_TIMES')){//被锁住，暂时不能登录
    			$this->status = 1111;
    			$this->msg = L('STATUS_1111');
    			$this->outPut();
    		}
    		 
    		$custInfo = $this->_cust->getCustInfoByUserName($username);
    		if(empty($custInfo)){
    			$this->status = 1113;
    			$this->msg = L('STATUS_1113');
    			$this->outPut();
    		}elseif($logintype!=933&&!\tools::verifyPwd($password , $custInfo['password'])){
    			$this->_mycache->set($failureTimesKey , $failureTimes+1);//密码错误登录次数累加1
    			$this->_mycache->expire($failureTimesKey , C('FORBID_LOGIN_EXPIRE'));
    			
    			$this->status = 1102;
    			$this->msg = L('STATUS_1102');
    			$this->outPut();
    		}elseif($custInfo['status'] == 0){
    			$this->status = 1103;
    			$this->msg = L('STATUS_1103');
    			$this->outPut();
    		}elseif($custInfo['status'] == 2){
    			$this->status = 1104;
    			$this->msg = L('STATUS_1104');
    			$this->outPut();
    		}elseif($custInfo['status'] == 3){
    			$this->status = 1105;
    			$this->msg = L('STATUS_1105');
    			$this->outPut();
    		}
    		 
    		$arr = array();
    		$arr['isonline'] = 1;
    		$arr['custid'] = $custInfo['custid'];
            $arr['zone'] = $custInfo['zone'];
            $arr['country'] = $custInfo['country'];
    		$arr['logintimes'] = $custInfo['logintimes']+1;
    		$arr['lastloginip'] = \tools::getClientIp();
    		$arr['lastlogintime'] = date("Y-m-d H:i:s");
    		if($logintype == 1) $arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    		if($logintype == 2) $arr['webauth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    		$ret = $this->_cust->updateCust($arr);
    		if($ret){
    			$ucrInfo = \ucr::bindUser($arr['custid']);
    			$arr['ucrtoken'] = $ucrInfo['token'];
    			$arr['ucropenid'] = $ucrInfo['open_id'];
    			
    			$this->_setCustInfoCache($arr);
    			
    			$custInfo = $this->_getCustInfo($custInfo['custid'] , 1);
    			
    			if($logintype == 1){
    				$this->_regTalkAuth($custInfo['custid'] , $arr['auth']);
    			}
    			
    			if($logintype == 2){
    				$this->_regTalkAuth($custInfo['custid'] , "" , $arr['webauth']);
    			}
    			
    			$this->_mycache->rm($failureTimesKey);
    			
    			if(!empty(C('WELCOME_LOGIN'))) $this->_sayWelcome($custInfo['custid']);
    			
    			$data = array();
    			$data['custid'] = $custInfo['custid'];
    			$data['expire'] = time()+C('TOKEN_EXPIRE');
    			$custInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    			
    			$custInfo['ucrtoken'] = $ucrInfo['token'];
    			$custInfo['ucropenid'] = $ucrInfo['open_id'];
    			
    			$this->data = $custInfo;
    			$this->outPut();
    		}else{
    			$this->status = 1106;
    			$this->msg = L('STATUS_1106');
    			$this->outPut();
    		}
    	}
    }
    
    
    /**
     * 短信登录,适用于不需要密码直接登录或注册
     */
    public function loginByMobileAction(){
    	$username = I('request.username');
    	$vercode = I('request.vercode');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$invitecode = (int)I('request.invitecode');
    	$this->_loginByMobile($username,$vercode,$time,$sign,$invitecode);
    }
    
    private function _loginByMobile($username,$vercode,$time,$sign,$invitecode=null){
    	if(empty($username) || empty($vercode) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(!\validate::isMobilePhoneNumber($username)){
    		$this->status = 1118;
    		$this->msg = L('STATUS_1118');
    		$this->outPut();
    	}
    	 
    	$this->_checkSign($username,$time,$sign);
    
    	$this->_checkVerCode($username , $vercode);
    	 
    	$custInfo = $this->_cust->getCustInfoByMobile($username);
    	if(!empty($custInfo)){//已经注册过
    		$this->_delVerCode($username);
    		
    		if($custInfo['status'] == 0){
    			$this->status = 1103;
    			$this->msg = L('STATUS_1103');
    			$this->outPut();
    		}elseif($custInfo['status'] == 2){
    			$this->status = 1104;
    			$this->msg = L('STATUS_1104');
    			$this->outPut();
    		}elseif($custInfo['status'] == 3){
    			$this->status = 1105;
    			$this->msg = L('STATUS_1105');
    			$this->outPut();
    		}
    		
    		$arr = array();
    		$arr['isonline'] = 1;
    		$arr['custid'] = $custInfo['custid'];
    		$arr['logintimes'] = $custInfo['logintimes']+1;
    		$arr['lastloginip'] = \tools::getClientIp();
    		$arr['lastlogintime'] = date("Y-m-d H:i:s");
    		$arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    		$ret = $this->_cust->updateCust($arr);
    		if($ret){
    			$ucrInfo = \ucr::bindUser($arr['custid']);
    			$arr['ucrtoken'] = $ucrInfo['token'];
    			$arr['ucropenid'] = $ucrInfo['open_id'];
    			
    			$this->_setCustInfoCache($arr);
    			
    			$custExtInfo['custid'] = $custInfo['custid'];
    			if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
    			$this->_cust_ext->saveCustExt($custExtInfo);
    			
    			$custInfo = $this->_getCustInfo($custInfo['custid'] , 1);
    			
    			$this->_regTalkAuth($custInfo['custid'] , $arr['auth']);
    			
    			$data = array();
    			$data['custid'] = $custInfo['custid'];
    			$data['expire'] = time()+C('TOKEN_EXPIRE');
    			$custInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    			 
    			$custInfo['ucrtoken'] = $ucrInfo['token'];
    			$custInfo['ucropenid'] = $ucrInfo['open_id'];
    			
    			$this->data = $custInfo;
    			$this->outPut();
    		}else{
    			$this->status = 1106;
    			$this->msg = L('STATUS_1106');
    			$this->outPut();
    		}
    	}
    	
    	 
    	$arr = array();
    	if(!empty($invitecode)) $arr['pid'] = $invitecode;
    	$arr['mobile'] = $username;
    	$arr['nickname'] = substr_replace($username,'*****',3,5);
    	$arr['isonline'] = 1;
    	$arr['logintimes'] = 1;
    	$arr['lastloginip'] = \tools::getClientIp();
    	$arr['lastlogintime'] = date("Y-m-d H:i:s");
    	$arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    	$arr['status'] = C('DEFAULT_CUST_STATUS');
    	$arr['idenum'] = $this->_getIdeNum();
    	$custid = $this->_cust->addCust($arr);
    	if($custid){
    		$this->_delVerCode($username);
    		
    		$arr['custid'] = $custid;
    		
    		$ucrInfo = \ucr::bindUser($custid);
    		$arr['ucrtoken'] = $ucrInfo['token'];
    		$arr['ucropenid'] = $ucrInfo['open_id'];
    		
    		$this->_setCustInfoCache($arr);
    		
    		$custInfo = $this->_getCustInfo($custid , 1);
			
			$this->_regTalkAuth($custInfo['custid'] , $arr['auth']);
			
			$data = array();
			$data['custid'] = $custid;
			$data['expire'] = time()+C('TOKEN_EXPIRE');
			$custInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    		
			$custInfo['ucrtoken'] = $ucrInfo['token'];
			$custInfo['ucropenid'] = $ucrInfo['open_id'];
			
    		$this->data = $custInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1106;
    		$this->msg = L('STATUS_1106');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 登出
     */
    public function logoutAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_logout($username,$time,$sign);
    }
    
    private function _logout($username,$time,$sign){
    	if(empty($username) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$this->_checkSign($username,$time,$sign);
    	 
    	$custInfo = $this->_cust->getCustInfoByUserName($username);
    	if(empty($custInfo)){
    		$this->status = 1113;
    		$this->msg = L('STATUS_1113');
    		$this->outPut();
    	}
    	 
    
    	$arr = array();
    	$arr['custid'] = $custInfo['custid'];
    	$arr['isonline'] = 0;
    	$ret = $this->_cust->updateCust($arr);
    	if($ret){
    		$this->_setCustInfoCache($arr);
    
    		$this->outPut();
    	}else{
    		$this->status = 1107;
    		$this->msg = L('STATUS_1107');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 检查用户是否存在
     */
    public function checkUserExistAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
//    	if(\validate::isMobilePhoneNumber($username)) $this->_checkUserExist($username,$time,$sign,1);//检查手机号
//    	if(\validate::isEmail($username)) $this->_checkUserExist($username,$time,$sign,2);//检查邮箱
        $this->_checkUserExist($username,$time,$sign);
    	$this->outPut();
    }
    
    
    /**
     * 发送手机短信验证码
     */
    public function sendSmsCodeAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$tplid = I('request.tplid');
    	$this->_sendSmsCode($username,$time,$sign,$tplid);
    }
    
    private function _sendSmsCode($username,$time,$sign,$tplid=0){
    	if(empty($username) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(!\validate::isMobilePhoneNumber($username)){
    		$this->status = 1118;
    		$this->msg = L('STATUS_1118');
    		$this->outPut();
    	}
    
    	$this->_checkSign($username,$time,$sign);
    
    	//检查手机发送验证码是否超出次数
    	$sendCodeKey = \cachekey::$key['BASE_KEY_CONF']['SEND_CODE_COUNT'].$username;
    	$sendCodeCnt = (int)$this->_mycache->get($sendCodeKey);
    	if($sendCodeCnt > (int)C('PER_DAY_MAX_SEND_TIMES')){
    		$this->status = 1108;
    		$this->msg = L('STATUS_1108');
    		$this->outPut();
    	}
    
    	//随机生成验证码
    	$vercode = \tools::genRandomNum(6);
    	//$vercode = 11111;
    	//$ret = true;
    	
    	if(empty($tplid)) $tplid = 93851;//53038 , 50530  //快聊 93851   //原子 99552
    	$ret = \sms::yuntongxun($username, array($vercode , C('VERCODE_EXPIRE')/60), $tplid);
    	
    	//$tplid = 21310017;
    	//$ret = \sms::qingma($username, $vercode, $tplid);
    	
    	//$ret = \sms::chuanglan($username, "您的验证码是：{$vercode}，请完成注册。", true);
    	//$ret = \sms::xunmai($username, "您的验证码是:{$vercode}. 请不要把验证码泄露给其他人.如非本人操作,可不用理会! [迅脉优力迪]");
    	
    	if($ret){
    		//记录成功发送短信的手机号
    		if(empty($this->_verifycode)){
    			$this->_verifycode = new \Common\Model\VerifyCodeModel();
    		}
    		$data['mobile'] = $username;
    		$data['content'] = $vercode;
    		$this->_verifycode->addVerifyCode($data);
    		
    		$this->_mycache->set($sendCodeKey , $sendCodeCnt+1);//发送短信次数累加1
    		$this->_mycache->expire($sendCodeKey , C('FORBID_SEND_CODE_EXPIRE'));
    		
    		$verKey = \cachekey::$key['BASE_KEY_CONF']['VERIFY_SMS_CODE'].$username;
    		$this->_mycache->set($verKey , $vercode);//缓存短信验证码
    		$this->_mycache->expire($verKey , C('VERCODE_EXPIRE'));
    		 
    		if(__ENV__ == "production"){
    			$this->outPut();
    		}else{
    			$this->data = array('vercode'=>$vercode);
    			$this->outPut();
    		}
    	}else{
    		$this->status = 1115;
    		$this->msg = L('STATUS_1115');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 邮件验证码
     */
    public function sendEmailCodeAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_sendEmailCode($username,$time,$sign);
    }
    
    private function _sendEmailCode($username,$time,$sign){
    	if(empty($username) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(!\validate::isEmail($username)){
    		$this->status = 1117;
    		$this->msg = L('STATUS_1117');
    		$this->outPut();
    	}
    	
    	$this->_checkSign($username,$time,$sign);
    	
    	//随机生成验证码
    	$vercode = \tools::genRandomNum(4);
    	$verKey = \cachekey::$key['BASE_KEY_CONF']['VERIFY_MAIL_CODE'].$username;
    	$this->_mycache->set($verKey , $vercode);//缓存验证码
    	$this->_mycache->expire($verKey , C('VERCODE_EXPIRE'));
    	
    	//send email
    	$res = \tools::send_mail($username, "", L('EMAIL_VERCODE_SUBJECT'), L('EMAIL_VERCODE_CONTENT' , array('vercode'=>$vercode)));
    	if($res!==true){
    		$this->status = 1117;
    		$this->msg = $res;
    		$this->outPut();
    	}
    	$this->outPut();
    }
    
    
    /**
     * 图形验证码
     */
    public function sendImgCodeAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_sendImgCode($username,$time,$sign);
    }
    
    private function _sendImgCode($username,$time,$sign){
    	if(empty($username) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$this->_checkSign($username,$time,$sign);
    
    	//随机生成验证码
    	$vercode = \tools::genRandomNum(4);
		$verKey = \cachekey::$key['BASE_KEY_CONF']['VERIFY_IMG_CODE'].$username;
		$this->_mycache->set($verKey , $vercode);//缓存验证码
		$this->_mycache->expire($verKey , C('VERCODE_EXPIRE'));
    		 
		$Verify = new \Think\Verify();
		$Verify->length   = 4;
		$Verify->__set('fontSize',24);//字体大小
		$Verify->__set('imageH',60);//图片高度
		$Verify->__set('imageW',200);//图片宽度
		$Verify->appEntry($vercode);
    }


    /**
     * 添加用户
     */
    public function addUserAction(){
        $username = I('request.username');
        $password = I('request.password');
        $email = I('request.email');
        $mobile = I('request.mobile');
        $country = I('request.country');
        $zone = I('request.zone');
        $nickname = I('request.nickname');
       // $regtype = I('request.regtype');
        $this->_addUser($username,$email,$mobile,$country,$zone,$password,$nickname);
    }
    private function _addUser($username,$email,$mobile,$country,$zone,$password,$nickname,$invitecode=null){
        if((empty($username)&&empty($email)&&empty($mobile) )){
            $this->status = 1000;
            $this->msg = L('STATUS_1000');
            $this->outPut();
        }
        //$this->_checkUserExist($username, $time, $sign);
        $custInfoExist =  $this-> _checkUserExistAndReturnWith($username,$mobile,$email);
        if (!empty($custInfoExist)) {
            $ucrInfo = \ucr::bindUser($custInfoExist["custid"]);
            $custInfoExist["ucrtoken"] = $ucrInfo['token'];
            $custInfoExist["ucropenid"] = $ucrInfo['open_id'];
            $custInfoExist["password"] = "";
            $this->status = 1112;
            $this->msg = L('STATUS_1112');
            $this->data = $custInfoExist;
            $this->outPut();
        }

        $password = \tools::encodePwd($password);

        $arr = array();
        if(!empty($invitecode)) $arr['pid'] = $invitecode;
        $arr['idenum'] = $this->_getIdeNum();
//        if($regtype == 1){
//            $arr['mobile'] = $username;//手机号注册
//            $arr['nickname'] = empty($nickname)?substr_replace($username,'*****',3,5):$nickname;
//        }else{
//            $arr['email'] = $username;//邮箱注册
//            $pos = strpos($username , '@' , 1);
//            $arr['nickname'] = substr($username , 0 , $pos);
//        }
        $arr['username'] = $username;
        if(!empty($mobile)) $arr['mobile'] = $mobile;
        if(!empty($email)) $arr['email'] = $email;
        $arr['nickname'] = $nickname;
        $arr['country'] = $country;
        $arr['zone'] = $zone;
        $arr['password'] = $password;
        $arr['isonline'] = 1;
        $arr['logintimes'] = 1;
        $arr['lastloginip'] = \tools::getClientIp();
        $arr['lastlogintime'] = date("Y-m-d H:i:s");
        $arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
        $arr['webauth'] = $arr['auth'];
        $arr['status'] = C('DEFAULT_CUST_STATUS');
        $custid = $this->_cust->addCust($arr);
        if($custid){
            $arr['custid'] = $custid;

            $ucrInfo = \ucr::bindUser($custid);
            $arr['ucrtoken'] = $ucrInfo['token'];
            $arr['ucropenid'] = $ucrInfo['open_id'];

            $this->_setCustInfoCache($arr);

            $custExtInfo['custid'] = $custid;
            if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
            $this->_cust_ext->saveCustExt($custExtInfo);

            $custInfo = $this->_getCustInfo($custid , 1);

            $this->_regTalkAuth($custInfo['custid'] , $arr['auth'] , $arr['webauth']);

            if(!empty(C('WELCOME_LOGIN'))){
                $this->_friend = new \Common\Model\FriendModel();
                $this->_friend->startTrans();
                $data = array();
                $data['custid'] = C('WELCOME_CUSTID');
                $data['tcustid'] = $custid;
                $data['status'] = 1;
                $ret1 = $this->_friend->addFriend($data);//对方将自己加为好友

                $data = array();
                $data['custid'] = $custid;
                $data['tcustid'] = C('WELCOME_CUSTID');
                $data['status'] = 1;
                $ret2 = $this->_friend->addFriend($data);//将对方加为自己的好友

                if($ret1 && $ret2){
                    $this->_friend->commit();
                }else{
                    $this->_friend->rollback();
                }

                //$this->_sayWelcome($custid);
            }

            $data = array();
            $data['custid'] = $custid;
            $data['expire'] = time()+C('TOKEN_EXPIRE');
            $custInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));

            $custInfo['ucrtoken'] = $ucrInfo['token'];
            $custInfo['ucropenid'] = $ucrInfo['open_id'];

            $this->data = $custInfo;
            $this->outPut();
        }else{
            $this->status = 1110;
            $this->msg = L('STATUS_1110');
            $this->outPut();
        }
    }
    
    /**
     * 注册
     */
    public function registerAction(){
    	$username = I('request.username');
    	$password = I('request.password');
    	$nickname = I('request.nickname');
    	$vercode = I('request.vercode');
        $country = I('request.country');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$regtype = I('request.regtype');
    	$invitecode = (int)I('request.invitecode');
    	$this->_register($country,$username,$password,$nickname,$vercode,$time,$sign,$regtype,$invitecode);
    }
    
    private function _register($country,$username,$password,$nickname,$vercode,$time,$sign,$regtype=1,$invitecode=null){
    	$regtype = empty($regtype) ? 1 : $regtype;
    	if(empty($username) || empty($password) || empty($vercode) || empty($time) || empty($sign) || !in_array($regtype , array(1,2,3))){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkVerCode($username , $vercode , $regtype);
    
    	$this->_checkUserExist($username, $time, $sign);
    
    	$password = \tools::encodePwd($password);
    
    	$arr = array();
    	if(!empty($invitecode)) $arr['pid'] = $invitecode;
    	$arr['idenum'] = $this->_getIdeNum();
    	if($regtype == 1){
    		$arr['mobile'] = $username;//手机号注册
    		$arr['nickname'] = empty($nickname)?substr_replace($username,'*****',3,5):$nickname; 
    	}else{
    		$arr['email'] = $username;//邮箱注册
    		$pos = strpos($username , '@' , 1);
    		$arr['nickname'] = substr($username , 0 , $pos);
    	}
    	$arr['password'] = $password;
    	$arr['isonline'] = 1;
        $arr['country'] = $country;
    	$arr['logintimes'] = 1;
    	$arr['lastloginip'] = \tools::getClientIp();
    	$arr['lastlogintime'] = date("Y-m-d H:i:s");
    	$arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    	$arr['webauth'] = $arr['auth'];
    	$arr['status'] = C('DEFAULT_CUST_STATUS');
    	$custid = $this->_cust->addCust($arr);
    	if($custid){
    		$this->_delVerCode($username);
    		
    		$arr['custid'] = $custid;
    		
    		$ucrInfo = \ucr::bindUser($custid);
    		$arr['ucrtoken'] = $ucrInfo['token'];
    		$arr['ucropenid'] = $ucrInfo['open_id'];
    		
    		$this->_setCustInfoCache($arr);
    		
    		$custExtInfo['custid'] = $custid;
    		if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
    		$this->_cust_ext->saveCustExt($custExtInfo);
    		
    		$custInfo = $this->_getCustInfo($custid , 1);
    		
    		$this->_regTalkAuth($custInfo['custid'] , $arr['auth'] , $arr['webauth']);
    		
    		if(!empty(C('WELCOME_LOGIN'))){
    			$this->_friend = new \Common\Model\FriendModel();
    			$this->_friend->startTrans();
    			$data = array();
    			$data['custid'] = C('WELCOME_CUSTID');
    			$data['tcustid'] = $custid;
    			$data['status'] = 1;
    			$ret1 = $this->_friend->addFriend($data);//对方将自己加为好友
    			
    			$data = array();
    			$data['custid'] = $custid;
    			$data['tcustid'] = C('WELCOME_CUSTID');
    			$data['status'] = 1;
    			$ret2 = $this->_friend->addFriend($data);//将对方加为自己的好友
    			
    			if($ret1 && $ret2){
    				$this->_friend->commit();
    			}else{
    				$this->_friend->rollback();
    			}
    			
    			$this->_sayWelcome($custid);
    		}
    		
    		$data = array();
    		$data['custid'] = $custid;
    		$data['expire'] = time()+C('TOKEN_EXPIRE');
    		$custInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    		
    		$custInfo['ucrtoken'] = $ucrInfo['token'];
    		$custInfo['ucropenid'] = $ucrInfo['open_id'];
    		
    		$this->data = $custInfo;
    		$this->outPut();
    	}else{
    		$this->status = 1110;
    		$this->msg = L('STATUS_1110');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 修改密码
     */
    public function updatePwdAction(){
    	$username = I('request.username');
    	$password = I('request.password');
    	$vercode = I('request.vercode');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$regtype = I('request.regtype');
    	$this->_updatePwd($username,$password,$vercode,$time,$sign , $regtype);
    }
    
    private function _updatePwd($username,$password,$vercode,$time,$sign , $regtype=1){
    	$regtype = empty($regtype) ? 1 : $regtype;
    	if(empty($username) || empty($password) || empty($vercode) || empty($time) || empty($sign) || !in_array($regtype , array(1,2,3))){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    
    	$this->_checkSign($username,$time,$sign);
    	 
    	$this->_checkVerCode($username , $vercode , $regtype);
    
    	$custInfo = $this->_cust->getCustInfoByUserName($username);
    	if(empty($custInfo)){
    		$this->status = 1113;
    		$this->msg = L('STATUS_1113');
    		$this->outPut();
    	}
    
    	$arr = array();
    	$arr['custid'] = $custInfo['custid'];
    	$arr['password'] = \tools::encodePwd($password);
    	$ret = $this->_cust->updateCust($arr);
    	if($ret){
    		$this->_delVerCode($username);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1114;
    		$this->msg = L('STATUS_1114');
    		$this->outPut();
    	}
    }
    
    private function _regTalkAuth($custid , $auth="" , $webauth=""){
    	if(empty(C('IM_SWITCH'))) return;
    			
    	$redisArr = C('CHAT_REDIS_DSN');
    	$custRedisIdx = \tools::talkCacheHash($custid , count($redisArr));
    	if(!empty(C('IM_SWITCH'))) $this->_talkcache[$custRedisIdx] = S($redisArr[$custRedisIdx]);
    	if(empty($this->_talkcache[$custRedisIdx])){
    		$this->outPut();
    	}else{
    		if(!empty($auth)) $this->_talkcache[$custRedisIdx]->set(\cachekey::$key['TALK_KEY_CONF']['TALK_AUTH'] . $custid , $auth);//设置talk auth
    		if(!empty($webauth)) $this->_talkcache[$custRedisIdx]->set(\cachekey::$key['TALK_KEY_CONF']['TALK_WEBAUTH'] . $custid , $webauth);//设置talk webauth
    	}
    }
    
    private function _sayWelcome($custid){
    	if($custid == C('WELCOME_CUSTID')) return;
		$sysCustInfo = $this->_getCustInfo(C('WELCOME_CUSTID') , 1);
		
		$msg['from_cust_name'] = $sysCustInfo['nickname'];
		$msg['from_cust_id'] = (int)C('WELCOME_CUSTID');
		$msg['to_cust_id'] = (int)$custid;
		$msg['type'] = 1;
		$msg['session_type'] = 1;
		$msg['session_id'] = (int)C('WELCOME_CUSTID');
		$msg['content'] = L('WELCOME_MSG');
		$msg['ext_content'] = json_encode(array());
		$msg['operationType'] = 4;//公众号
		//sleep(3);
		\im::sendMsg(3, array($custid), array(), $msg);
    }
}
